<?php

namespace App\Http\Controllers\web;


use App\Http\Controllers\Controller;
use App\Models\StoreCouponUser;
use App\Models\StoreOrder;
use App\Models\SystemCity;
use App\Models\UserAddress;
use App\Repository\RepositoryInterface\PersonalInterface;
use Illuminate\Http\Request;


//个人主页
class PersonalController extends Controller
{
    //
    protected $personal;
    public function __construct(PersonalInterface $personal){
        $this->personal = $personal;
    }

    /**
     * @param Request $request
     * @return \think\response\Json|void
     * 个人中心页面
     */

    public function index(Request $request){
        $uid=$request->get('uid');
        return $this->personal->index($uid);
    }


    /**
     * 团长新增方法
     */
    public function colonelCreate(Request $request){
        $param=$request->post();
        return $this->personal->colonelCreate($param);
    }


    /**
     * 用户分享 邀请
     */

    public function shareUser(Request $request){
        $share=$request->get('account');

        return $this->personal->shareUser($share);
    }

 


    /**
     * 退款数目和金额统计
     */
    public function refund(Request $request){
        $uid=$request->get('uid');
        $price=0;
        $order=StoreOrder::where('uid',$uid)->
        where('refund_status','=','2')->get();
       foreach ($order as $key=>$v){
           $price=$price+$v['pay_price'];
       }

        return ['code'=>200,'data'=>'您的总退款订单数为'.count($order).'总金额为'.$price,'msg'=>'查询成功'];
    }

    /**
     * 优惠券列表开发
     */
    public function discountIndex(Request $request){
        $uid=$request->get('uid');
        $discount=StoreCouponUser::where('uid',$uid)->get();
        if(!$discount){
            return fail('200',$discount,'修改成功');
        }else{
            return success('200',$discount,'查询成功');
        }
    }

    /**
     *
     * 地址列表
     */
    public function siteIndex(Request $request){
        $uid=$request->get('uid');
        $site=UserAddress::where('uid',$uid)->get();
        if(!$site){
            return fail('200',$site,'修改成功');
        }else{
             return json_encode(['code'=>200,'data'=>$site,'msg'=>'成功']);
        }
    }


    /**
     * 修改默认地址
     */
    public function siteDefault(Request $request){
        $id=$request->get('id');
        $default=UserAddress::where('id',$id)->update(['is_default'=>1]);
        if(!$default){
            return fail('200',$default,'修改失败');
        }else{
            return success('200',$default,'修改成功');
        }
    }

    /**
     * 添加收货地址
     */
    public function siteAdd(Request $request){
        $param=$request->post();
        $site=UserAddress::create($param);
        if(!$site){
            return error('500',$site,'添加失败');
        }else{
       return json_encode(['code'=>200,'data'=>$site,'msg'=>'成功']);
        }
    }



        public function maket(Request $request)
        {
            //取得传过来的ID值
            $id = $request->get('id');
            //将ID值进行pid的判断操作
            $showMaket =SystemCity::where('pid',$id)->get();
            return success($showMaket);
        }

    public function bstr(Request $request)
    {
        //取得传过来的ID值
        $id = $request->all('id');
        //将ID值进行pid的判断操作
        $showMaket = SystemCity::where('pid',$id)->get();
        return success($showMaket);
    }
}
